package com.been.common.model;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 所有实体对象的基类，有个抽象方法getId--以字符串形式返回该实体的主键， 并重写了Object的equals、toString以及hashCode方法
 * 
 * @author been 2011-09-27
 */
public abstract class BaseModel implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 以字符串形式返回该实体的主键
	 * 
	 * @return String 主键
	 */
	public abstract String getId();

	/*
	 * 重写 equals 比较ID (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 * 
	 * @param object
	 * 
	 * @return 比较后的boolean值
	 */
	@Override
	public boolean equals(Object object) {
		if (this == object) {
			return true;
		}
		if (object instanceof BaseModel) {
			BaseModel baseModel = (BaseModel) object;
			return !(getId() != null ? !getId().equals(baseModel.getId())
					: baseModel.getId() != null);
		}
		return false;
	}

	/*
	 * 返回ID的hashCode (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode() return int ID的hashCode
	 */
	public int hashCode() {
		return getId() != null ? getId().hashCode() : 0;
	}

	/*
	 * 返回该实体的主键的String表示形式 (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 * 
	 * @return String 返回的ID字符串
	 */
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(
				getId()).toString();
	}
}
